package es.uniovi.miw.tfm.scowt.stores.jena;

import static es.uniovi.miw.tfm.scowt.stores.jena.util.UriManager.HAS_KNOWLEDGE;
import static es.uniovi.miw.tfm.scowt.stores.jena.util.UriManager.HAS_SKILL;
import static es.uniovi.miw.tfm.scowt.stores.jena.util.UriManager.HAS_SKILL_DESCR;
import static es.uniovi.miw.tfm.scowt.stores.jena.util.UriManager.HAS_KNOW_DESCR;
import static es.uniovi.miw.tfm.scowt.stores.jena.util.UriManager.NS;

import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.NodeIterator;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;

import es.uniovi.miw.tfm.scowt.model.Competence;
import es.uniovi.miw.tfm.scowt.model.Knowledge;
import es.uniovi.miw.tfm.scowt.model.Skill;

@Component
public class CompetenceJenaDao extends AbstractJenaDao {

    @Override
    public Competence findCompetenceByIdAndLevel(String competenceId, Integer level) {
	// Check request
	validate(competenceId);

	// Prepare the response
	Competence competence = new Competence();
	competence.setSkillsList(getSkillsList(competenceId));
	competence.setKnowledgeList(getKnowledgeList(competenceId));
	return competence;
    }

    private void validate(String competenceId) {
	if (competenceId != null) {
	    Model model = getModel();
	    Resource competenceResource = model.createResource(NS + competenceId);
	    if (!model.containsResource(competenceResource)) {
		throw new IllegalArgumentException();
	    }
	} else {
	    throw new IllegalArgumentException();
	}

    }

    private List<Knowledge> getKnowledgeList(String competenceId) {
	List<Knowledge> knowledgeList = new LinkedList<Knowledge>();
	Model model = getModel();
	Resource competenceResource = model.getResource(NS + competenceId);
	Property hasKnowledge = model.getProperty(HAS_KNOWLEDGE);
	if (hasKnowledge != null) {
	    NodeIterator iterator = model.listObjectsOfProperty(competenceResource, hasKnowledge);
	    while (iterator.hasNext()) {
		Resource skillResource = (Resource) iterator.next();
		// Buscamos los datos del knowledge
		Property hasKnowledgeDescription = model.getProperty(HAS_KNOW_DESCR);
		NodeIterator descrIterator = model.listObjectsOfProperty(skillResource,
			hasKnowledgeDescription);
		// Poblamos el nuevo Knowledge
		Knowledge knowledge = new Knowledge();
		while (descrIterator.hasNext()) {
		    RDFNode description = descrIterator.next();
		    knowledge.setDescription(description.asLiteral().getString());
		}
		knowledgeList.add(knowledge);
	    }
	}
	return knowledgeList;
    }

    private List<Skill> getSkillsList(String competenceId) {
	List<Skill> skills = new LinkedList<Skill>();
	Model model = getModel();
	Resource competenceResource = model.getResource(NS + competenceId);
	// Retrieve the properties
	Property hasSkill = model.getProperty(HAS_SKILL);
	if (hasSkill != null) {
	    NodeIterator iterator = model.listObjectsOfProperty(competenceResource, hasSkill);
	    while (iterator.hasNext()) {
		Resource skillResource = (Resource) iterator.next();
		// Buscamos los datos del skill
		Property hasSkillDescription = model.getProperty(HAS_SKILL_DESCR);
		NodeIterator descrIterator = model.listObjectsOfProperty(skillResource,
			hasSkillDescription);
		// Poblamos el nuevo Skill
		Skill skill = new Skill();
		while (descrIterator.hasNext()) {
		    RDFNode description = descrIterator.next();
		    skill.setDescription(description.asLiteral().getString());
		}
		skills.add(skill);
	    }
	}
	return skills;
    }

    @Override
    public List<Competence> getAllCompetencies() {
	// TODO Auto-generated method stub
	return null;
    }

}
